package com.industrial.premu.controller;

import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.web.page.TableDataInfo;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.common.security.annotation.RequiresRoles;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.PreMuCompanyTax;
import com.industrial.premu.service.IPreMuCompanyTaxService;
import com.industrial.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

/**
 * 企业自已填写的税收Controller
 *
 * @author SangGuangCan
 * @date 2024-05-20
 */
@RestController
@RequestMapping("/slef/write")
public class PreMuCompanyTaxController extends BaseController
{
    @Autowired
    private IPreMuCompanyTaxService preMuCompanyTaxService;

    /**
     * 查询企业自已填写的税收列表
     */
    @RequiresRoles(value = "x_01_011")
    @GetMapping("/list")
    public TableDataInfo list(PreMuCompanyTax preMuCompanyTax)
    {
        startPage();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        if(sysUser.isAdmin()){
            preMuCompanyTax.setCounty("370000");
        }
        List<PreMuCompanyTax> list = preMuCompanyTaxService.selectPreMuCompanyTaxList(preMuCompanyTax);
        return getDataTable(list);
    }

    /**
     * 导出企业自已填写的税收列表
     */
    @Log(title = "企业自已填写的税收", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuCompanyTax preMuCompanyTax)
    {
        List<PreMuCompanyTax> list = preMuCompanyTaxService.selectPreMuCompanyTaxList(preMuCompanyTax);
        ExcelUtil<PreMuCompanyTax> util = new ExcelUtil<PreMuCompanyTax>(PreMuCompanyTax.class);
        util.exportExcel(response, list, "企业自已填写的税收数据");
    }

    /**
     * 获取企业自已填写的税收详细信息
     */
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(preMuCompanyTaxService.selectPreMuCompanyTaxByBaseId(baseId));
    }

    @RequiresRoles(value = "q_01_001")
    @GetMapping(value = "/show")
    public AjaxResult getCompanyInfoByCompanyId()
    {
        return success(preMuCompanyTaxService.getCompanyInfoByCompanyId());
    }

    /**
     * 新增企业自已填写的税收
     */
    @RequiresRoles(value = "q_01_001")
    @Log(title = "企业自已填写的税收", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuCompanyTax preMuCompanyTax)
    {
        LocalDate localDate = LocalDate.now();
        preMuCompanyTax.setYears(String.valueOf(localDate.getYear() - 1));
        //获取Uscc 这里企业的用户名就是Uscc
        String username = SecurityUtils.getUsername();
        preMuCompanyTax.setUscc(username);
        return toAjax(preMuCompanyTaxService.insertPreMuCompanyTax(preMuCompanyTax));
    }

    /**
     * 修改企业自已填写的税收
     */
    @Log(title = "企业自已填写的税收", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuCompanyTax preMuCompanyTax)
    {
        return toAjax(preMuCompanyTaxService.updatePreMuCompanyTax(preMuCompanyTax));
    }


    @PutMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody PreMuCompanyTax preMuCompanyTax)
    {
        return toAjax(preMuCompanyTaxService.updateStatus(preMuCompanyTax));
    }

    /**
     * 批量通过审核
     * @param usccs 社会统一信用代码
     * @return
     */
    @PutMapping("/batchUpdateStatus")
    public AjaxResult batchUpdateStatus(@RequestBody List<String> usccs){
        if(usccs == null || usccs.isEmpty()){
            return AjaxResult.warn("没有找到社会统一信用代码");
        }
        LocalDate localDate = LocalDate.now();
        preMuCompanyTaxService.batchUpdateStatus(usccs,String.valueOf(localDate.getYear() - 1));
        return AjaxResult.success();
    }

    /**
     * 删除企业自已填写的税收
     */
    @Log(title = "企业自已填写的税收", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(preMuCompanyTaxService.deletePreMuCompanyTaxByBaseIds(baseIds));
    }
}